package com.c1g19.action.illness;

import com.c1g19.action.BaseAction;
import com.c1g19.service.illness.CaseService;
import com.c1g19.utils.DbControl;
import com.c1g19.utils.JsonUtils;
import com.c1g19.utils.Struts2Utils;
import org.apache.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.*;
import java.util.List;
import java.util.Map;

/**
 * 用以处理病例管理的Action
 * Created by Mr_C on 2017/3/8.
 */
public class CaseAction extends BaseAction {
    private static final long serialVersionUID = 1L;
    private static final Logger logger = Logger.getLogger(CaseAction.class);

    int case_id = 0;
    int pro_id = 0;
    int flow_id = 0;
    String flow_name = "";

    /**
     * getter and setter
     */
    public int getCase_id() {
        return case_id;
    }

    public void setCase_id(int case_id) {
        this.case_id = case_id;
    }

    public int getPro_id() {
        return pro_id;
    }

    public void setPro_id(int pro_id) {
        this.pro_id = pro_id;
    }

    public int getFlow_id() {
        return flow_id;
    }

    public void setFlow_id(int flow_id) {
        this.flow_id = flow_id;
    }

    public String getFlow_name() {
        return flow_name;
    }

    public void setFlow_name(String flow_name) {
        this.flow_name = flow_name;
    }

    /**
     * 跳转到病例学习页
     * @return
     */
    public String preViewCase(){
        return "success";
    }

    /**
     * 跳转病例详情页
     * @return
     */
    public String preCaseDetail(){
        getRequest().setAttribute("case_id", case_id);
        return "success";
    }

    /**
     * 得到总的病例分类表
     */
    public void getAll_Main_Case(){
        CaseService caseService = new CaseService();
        Map<String, Object> allMainCaseList = caseService.getAll_Main_Case();


        //发给网页执行操作
        String resultJsonString = changeListToJsonStringWithJtable(allMainCaseList);
        Struts2Utils.renderJson(resultJsonString);
    }

    /**
     * 通过病例ID分页得到
     */
    public void getCaseWithID(){
//        int beginIndex = getJtStartIndex();
//        int pageSize = getJtPageSize();
        String filter = "";

        //不进行分页读取
        int beginIndex = 0;
        int pageSize = 0;

        //此处添加filter操作
        CaseService caseService = new CaseService();
        Map<String, Object> CaseList = caseService.getCaseWithID(case_id, filter, beginIndex, pageSize);

        //发给网页执行操作
        String resultJsonString = changeListToJsonStringWithJtable(CaseList);
        Struts2Utils.renderJson(resultJsonString);
    }

    /**
     * 获得病例详细信息
     */
    public void getCaseDetail(){
        logger.info("开始查询相关病例，病例ID为:"+case_id);
        CaseService caseService = new CaseService();
        Map<String, Object> allMainCaseList = caseService.getCaseDetail(case_id);
        //发给网页执行操作
        JSONObject jsonObject = new JSONObject();
        try {

            for (String key : allMainCaseList.keySet()) {
                if(key.contains("_loc")&&!key.contains("_count")) {
                    List<String> loclist = (List<String>) allMainCaseList.get(key);
                    JSONArray locjsonarray = new JSONArray(loclist);
                    jsonObject.put(key, locjsonarray);
                }else{
                    jsonObject.put(key, allMainCaseList.get(key).toString());
                }
            }
        } catch (Exception e) {
            logger.error("", e);
        }
        String resultJsonString = jsonObject.toString();
        logger.info(resultJsonString);
        Struts2Utils.renderJson(resultJsonString);
    }

    /**
     * 跳转到角色扮演模块
     * @return
     */
    public String preViewCosplay(){
        return "success";
    }

    /**
     * 跳转职业详情页
     * @return
     */
    public String preProfessionDetail(){
        getRequest().setAttribute("pro_id", pro_id);
        return "success";
    }

    /**
     * 跳转到流程详情
     * @return
     */
    public String preFlowDetail() throws UnsupportedEncodingException{
        getRequest().setAttribute("flow_id", flow_id);
        flow_name = new String(flow_name.getBytes("ISO8859_1"), "UTF8");
        getRequest().setAttribute("flow_name", flow_name);
        return "success";
    }
    /**
     * 得到角色扮演的全部角色
     */
    public void getAllProfession(){
        CaseService caseService = new CaseService();
        Map<String, Object> allMainCaseList = caseService.getAllProfession();

        //发给网页执行操作
        String resultJsonString = changeListToJsonStringWithJtable(allMainCaseList);
        Struts2Utils.renderJson(resultJsonString);
    }

    /**
     * 获得职业相关详细信息
     */
    public void getProfessionDetail(){
        logger.info("开始查询相关职业，职业ID为:"+pro_id);
        CaseService caseService = new CaseService();
        Map<String, Object> proDetailList = caseService.getProfessionDetail(pro_id);
        //发给网页执行操作
        JSONObject jsonObject = new JSONObject();
        try {

            for (String key : proDetailList.keySet()) {
                if(key.equals("flow_id_list")) {
                    List<String> loclist = (List<String>) proDetailList.get(key);
                    JSONArray locjsonarray = new JSONArray(loclist);
                    jsonObject.put(key, locjsonarray);
                }else if(key.equals("relevant_office")||key.equals("flow_detail")) {
                    String res = proDetailList.get(key).toString();
                    JSONArray resArray;
                    resArray = new JSONArray(res);
                    jsonObject.put(key, resArray);
                }else{
                    jsonObject.put(key, proDetailList.get(key).toString());
                }
            }
        } catch (Exception e) {
            logger.error("", e);
        }
        String resultJsonString = jsonObject.toString();
        Struts2Utils.renderJson(resultJsonString);
    }

    /**
     * 获得流程相关详细信息
     */
    public void getFlowDetail(){
        logger.info("开始查询相关流程，流程ID为:"+flow_id);
        CaseService caseService = new CaseService();
        Map<String, Object> proDetailList = caseService.getFlowDetail(flow_id);
        //发给网页执行操作
        JSONObject jsonObject = new JSONObject();
        try {

            for (String key : proDetailList.keySet()) {
                if(key.equals("flow_id_list")) {
                    List<String> loclist = (List<String>) proDetailList.get(key);
                    JSONArray locjsonarray = new JSONArray(loclist);
                    jsonObject.put(key, locjsonarray);
                }else if(key.equals("relevant_office")||key.equals("flow_detail")) {
                    String res = proDetailList.get(key).toString();
                    JSONArray resArray;
                    resArray = new JSONArray(res);
                    jsonObject.put(key, resArray);
                }else{
                    jsonObject.put(key, proDetailList.get(key).toString());
                }
            }
        } catch (Exception e) {
            logger.error("", e);
        }
        String resultJsonString = jsonObject.toString();
        Struts2Utils.renderJson(resultJsonString);
    }

}
